package com.yifeng.repo.micro.service.server.auth.sso.filter.context;

import javax.servlet.http.HttpServletRequest;

/**
 * 鉴权上下文
 */
public class SsoContext {
    private static final ThreadLocal<SsoAuthInfo> LOCAL_AUTH_INFO = new ThreadLocal<>();

    public static void set(SsoAuthInfo ssoAuthInfo) {
        LOCAL_AUTH_INFO.set(ssoAuthInfo);
    }

    public static SsoAuthInfo get() {
        return LOCAL_AUTH_INFO.get();
    }

    public static void add(HttpServletRequest httpServletRequest) {
        set(OpenxHeaderParser.parser(httpServletRequest));
    }
    
    public static SsoAuthInfo getOrAdd(HttpServletRequest httpServletRequest) {
        SsoAuthInfo ssoAuthInfo = LOCAL_AUTH_INFO.get();
        if(ssoAuthInfo == null) {
           add(httpServletRequest);
        }
        return LOCAL_AUTH_INFO.get();
    }

    public static void clear() {
        LOCAL_AUTH_INFO.remove();
    }
}
